package com.tosit.driverschool.service;
import com.tosit.driverschool.dao.VehicleMapper;
import com.tosit.driverschool.dao.VehicleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import com.tosit.driverschool.common.entity.Vehicle;

@Service
public class VehicleServiceImpl implements VehicleService {

    @Autowired
    VehicleRepository vehicleRepository;
    @Autowired
    VehicleMapper vehicleMapper;

    @Override
    public short getmaxId() {
        return vehicleMapper.getMaxId();
    }

    @Override
    public int add(Vehicle vehicle) {
         return vehicleMapper.insert(vehicle);
    }

    @Override
    public void update(Vehicle vehicle) {
        vehicleMapper.updateByPrimaryKey(vehicle);
    }

    @Override
    public Vehicle selectbyId(short id) {
        return vehicleMapper.selectByPrimaryKey(id);
    }

    @Override
    public Set<String> getAllType(String subject) {
        List<Vehicle> all  = vehicleRepository.findAll();
        Set<String> allTypes = new HashSet<>();

        for (Vehicle vehicle : all) {
            if(subject.equals(vehicle.getSubjectId()))
            allTypes.add(vehicle.getVehicleType());
        }
        return allTypes;
    }

    @Override
    public Set<String> getAllSubject(String type) {
        List<Vehicle> all  = vehicleRepository.findAll();
        Set<String> allSubjects = new HashSet<>();
        for(Vehicle vehicle : all){
            allSubjects.add(vehicle.getSubjectId());
        }
        return allSubjects;
    }

    public List<Vehicle> findAll() {
        return vehicleMapper.findAll();
    }

    public void updateIsdelete(Vehicle vehicle) {
        vehicleMapper.updateByPrimaryKey(vehicle);
    }
}
